package com.zsqat.eshop.ware.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;

import com.zsqat.eshop.ware.dao.PurchaseDetailDao;
import com.zsqat.eshop.ware.entity.PurchaseDetailEntity;
import com.zsqat.eshop.ware.service.PurchaseDetailService;


@Service("purchaseDetailService" )
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<PurchaseDetailEntity>();

    String key = (String) params.get("key");
    if (!StringUtils.isEmpty(key)) {
      wrapper.and(x -> {
        x.eq("purchase_id", key).or().eq("sku_id", key);
      });
    }

    String status = (String) params.get("status");
    if ((!StringUtils.isEmpty(status)) && (!"0".equalsIgnoreCase(status))) {
      wrapper.eq("status", status);
    }

    String wareId = (String) params.get("wareId");
    if ((!StringUtils.isEmpty(wareId)) && (!"0".equalsIgnoreCase(wareId))) {
      wrapper.eq("ware_id", wareId);
    }

    IPage<PurchaseDetailEntity> page = this.page(
        new Query<PurchaseDetailEntity>().getPage(params),
        wrapper
    );

    return new PageUtils(page);
  }

  @Override
  public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
    List<PurchaseDetailEntity> purchaseId = this.list(
        new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));

    return purchaseId;
  }

}